import md5 from "js-md5"
import Point from "./point"
import { getNowImgPoint, hasLineIntersection } from "./mathFun"

export default class polygonTag {
    //id
    id = "";
    //颜色
    color = "yellow";
    //线条宽度
    width = 2;
    //显示
    show = true;
    //点位
    position = [];
    //绘制时的点位
    drawPosition = [];
    //绘制时移动的虚拟点
    vitural = {
        show: false
    };
    //边界
    border = [];
    //创建时间
    createdT = null;
    //高亮
    highLight = false;
    //标注中文
    zhcn = "";
    //标注藏文
    zy = "";
    //标题
    title = "";
    //绘制标题
    showTitle = false;

    constructor(Points, position, taggingConfig, vitural = false) {
        this.color = taggingConfig.color
        this.width = taggingConfig.width
        if (taggingConfig.objid) {
            this.id = taggingConfig.objid
        } else {
            this.id = md5(new Date().getTime().toString())
        }
        this.zhcn = taggingConfig.zhcn ? taggingConfig.zhcn : ""
        this.zhcn = taggingConfig.zy ? taggingConfig.zy : ""
        this.title = taggingConfig.title ? taggingConfig.title : ""
        this.createdT = taggingConfig.createdT ? taggingConfig.createdT : 0
        if (typeof Points == "object") {
            this.position = this.position.concat(JSON.parse(JSON.stringify(Points)))
            if (vitural == true) {
                this.vitural.show = true
                this.vitural.point = getNowImgPoint(Points[0].X, Points[0].Y, position)
            }
            this.drawPosition = this.position.map((item) => {
                return getNowImgPoint(item.X, item.Y, position);
            })
            this.getBorder()
        }
    }

    //添加点位
    addPoint(point, position) {
        this.position = this.position.concat(point)
        this.drawPosition.push(getNowImgPoint(point.X, point.Y, position))
        this.getBorder()
    }

    //设置虚拟点的位置
    resetVitural(x, y, position) {
        if (this.vitural.show) {
            this.vitural.point = getNowImgPoint(x, y, position)
        }
    }

    //关闭虚拟点
    closeVitural() {
        this.vitural.show = false
        this.vitural.point = null
    }

    //拖拽时自动重设点位信息
    dragAutoReset(position) {
        this.drawPosition = this.position.map((item) => {
            return getNowImgPoint(item.X, item.Y, position);
        })
        this.getBorder()
    }

    //计算边界
    getBorder() {
        let [bx1, by1, bx2, by2] = [1000000, 1000000, -1000000, -1000000]
        this.drawPosition.map((item) => {
            bx1 = item[0] < bx1 ? item[0] : bx1
            by1 = item[1] < by1 ? item[1] : by1
            bx2 = item[0] > bx2 ? item[0] : bx2
            by2 = item[1] > by2 ? item[1] : by2
        })
        this.border = [bx1, by1, bx2, by2]
    }

    //计算点位是否在标注中
    inTag(point) {
        let interPoints = 0;
        this.drawPosition.forEach((item, key) => {
            let temp = null
            if (key == this.drawPosition.length - 1) {
                temp = new Point(this.drawPosition[0][0], this.drawPosition[0][1])
            } else {
                temp = new Point(this.drawPosition[key + 1][0], this.drawPosition[key + 1][1])
            }
            interPoints += hasLineIntersection(new Point(item[0], item[1]), temp, point, new Point(point.X, 1000000))
        })
        return interPoints % 2 != 0;
    }

    get positionP() {
        let temp = this.position.map((item) => {
            return [item.X, item.Y];
        })
        return temp;
    }
}